package com.netease.task.controller;

import com.netease.task.meta.Book;
import com.netease.task.meta.Order;
import com.netease.task.service.impl.BookServiceImpl;
import com.netease.task.service.impl.OrderServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Controller
public class BookController {

    @Autowired
    private BookServiceImpl bookService;

    @Autowired
    private OrderServiceImpl orderService;

    @RequestMapping("/api/public")
    public String upload(){
        return "/user/public";
    }

    @RequestMapping("/api/publicSubmit")
    public String publicSubmit(String title, String summary, String image, String detail, BigDecimal price, Model model){
        Book book = new Book(title,summary,image,detail,price,0,0);
        bookService.addBook(book);
        Integer id = bookService.getIdByTitle(title);
        model.addAttribute("id",id);
        return "/user/success";
    }


    @RequestMapping("/api/show")
    public String show(@RequestParam(name = "id") Integer id,Model model){

        // 拿到当前登录的对象
        Subject subject = SecurityUtils.getSubject();
        String currentUser = (String) subject.getPrincipal();
        if("seller".equals(currentUser)){
            model.addAttribute("flag",1);  // 卖家
        }else if("buyer".equals(currentUser)){
            model.addAttribute("flag",2);  // 买家
        }else{
            model.addAttribute("flag",0);  // 未登录
        }
        Book book = bookService.getBookById(id);
        Order order = orderService.getOrderByBookId(id);
        model.addAttribute("id",id);
        model.addAttribute("title",book.getTitle());
        model.addAttribute("summary",book.getSummary());
        model.addAttribute("picture",book.getPicture());
        model.addAttribute("detail",book.getDetail());
        model.addAttribute("price",book.getPrice());
        model.addAttribute("isSell",book.getIsSell());
        model.addAttribute("number",book.getNumber());
        model.addAttribute("buyNum",0);
        model.addAttribute("buyPrice",0);
        if(order != null && order.getIsComplete() == 1){
            model.addAttribute("buyNum",order.getBuyNum());
            model.addAttribute("buyPrice",order.getBuyPrice());
        }
        return "/user/show";
    }

    @RequestMapping("/api/edit")
    public String edit(@RequestParam(name = "id") Integer id,Model model) {
        Book book = bookService.getBookById(id);
        model.addAttribute("book",book);
        return "/user/edit";
    }

    @RequestMapping("/api/editSubmit")
    public String editSubmit(@RequestParam(name = "id") Integer id,String title, String summary, String image, String detail, BigDecimal price,Model model){
        Book boo = bookService.getBookById(id);
        Book book = new Book(id,title,summary,image,detail,price,boo.getIsSell(),boo.getNumber());
        bookService.updateById(book);
        model.addAttribute("id",id);
        return "/user/editSuccess";
    }

    @RequestMapping("/api/delete")
    public String delete(@RequestParam(name = "id") Integer id){
        bookService.deleteBook(id);
        return "/api/sellerindex";
    }

    @RequestMapping("/api/noSell")
    public String noSell(Model model){
        List<Book> books = bookService.getBooksNoSell();
        model.addAttribute("books",books);
        return "/user/noSell";
    }

    @RequestMapping("/api/buy")
    public String buy(@RequestParam(name = "id") Integer id,Integer buyNum){
        // 加入购物车
        Book book = bookService.getBookById(id);
        Order orderExist = orderService.getOrderByBookId(id);
        if (orderExist != null) {
            orderService.updateOrderByBookId(id,buyNum);
        } else {
            Order order = new Order(book.getId(),book.getPicture(),book.getTitle(),buyNum,book.getPrice(),new Date(),0);
            orderService.addOrder(order);
        }
        return "/api/toSettleAccount";
    }

    // 确认购买
    @RequestMapping("/api/confirmBuy")
    public String confirmBuy(){
        List<Order> orders = orderService.getOrderByType(0);
        for (Order order : orders){
            int bookId = order.getBookId();
            bookService.updateIsSell(bookId);
        }
        orderService.updateIsComplete(new Date());
        return "/api/toAccount";
    }

    @RequestMapping("/api/upload")
    @ResponseBody
    public String localUpload(@RequestParam("file") MultipartFile file){
        System.out.println("本地上传");
        return "/user/fileUpload";

    }





}
